<?php

Raise::load('core.RaiseDomParser');

/**
 * TestCoreRaiseDomParser class
 * Testing unit of 'phpraise/core/RaiseDomParser.php'
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseDomParser extends RaiseTest {

    public static function testClass(){
        self::assertClassExists('RaiseDomParser');
        self::assertClassExtends('RaiseDomParser', 'RaiseStaticClass');
        self::assertClassExists('RaiseDomTreeNode');
        self::assertClassExtends('RaiseDomTreeNode', 'RaiseTreeNode');
    }

    public static function testParser(){
        $html = '<div class="test" id="mainDiv">Hello <b>there</b>.</div>';
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree->children()->get(0), 'RaiseDomTreeNode');
        self::assertStrictEqual($tree->children()->get(0)->tag()->tagName(), 'div');
        self::assertStrictEqual($tree->children()->get(0)->tag()->attributes()->count(), 2);
    }

    public static function testSelectors(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseDomTreeNode');
        
        self::assertStrictEqual($tree->select('div.list')->children()->get(0)->tag()->attributes()->toArray(), array('class'=>'list'));
        self::assertStrictEqual($tree->children()->get(0)->tag()->tagName(), $tree->select('div.list')->children()->get(0)->tag()->tagName());
        self::assertStrictEqual($tree->select('ul li')->children()->get(1)->tag()->value(), 'Second line');
        self::assertStrictEqual($tree->select('ul:first li:last')->tag()->value(), 'Fourth line');
        self::assertStrictEqual($tree->select('ul:first li#highlight')->tag()->value(), 'Third line');
        self::assertStrictEqual($tree->select('ul:first li#highlight')->tag()->value(), $tree->select('#highlight')->tag()->value());
    }

    public static function testGetByElementId(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseDomTreeNode');

        self::assertStrictEqual($tree->getByElementId('highlight'), $tree->select('#highlight'));
        self::assertStrictEqual($tree->getByElementId('highlight')->tag()->value(), 'Third line');
        self::assertStrictEqual($tree->getByElementId('highlight')->tag()->attributes()->count(), 1);
        self::assertStrictEqual($tree->getByElementId('header')->tag()->attributes()->count(), 2);
    }

    public static function testGetByTagName(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseDomTreeNode');

        // comparing objects cannot use ===, use == instead
        self::assertStrictEqual($tree->getByTagName('li')->count(), 4);
        self::assertStrictEqual($tree->getByTagName('div')->count(), 2);
        self::assertStrictEqual($tree->getByTagName('div')->get(0)->children()->get(0), $tree->getByTagName('div')->get(1));
        self::assertObjectClass($tree->getByTagName('div')->get(0)->tag(), 'RaiseHtmlTag');
        self::assertStrictEqual($tree->getByTagName('ul')->count(), 1);
        self::assertStrictEqual($tree->getByTagName('ul')->get(0)->children()->count(), 4);
    }

    public static function testGetByAttrValue(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';
        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseDomTreeNode');

        self::assertStrictEqual($tree->getByAttrValue('href','#top')->count(), 1);
        self::assertStrictEqual($tree->getByAttrValue('href','#top')->get(0), $tree->getByTagName('a')->get(0));
        self::assertStrictEqual($tree->getByAttrValue('class','list')->get(0), $tree->children()->get(0));
    }


    public static function testGetByClass(){
        $html = '<div class="list"><div id="header" class="subtitle hdr">This is a list</div><ul><li><a href="#top" target="_blank" title="Go > Top">First line</a></li><li class="hdr">Second line</li><li id="highlight">Third line</li><li>Fourth line</li></ul></div>';

        $tree = RaiseDomParser::parseString($html);
        self::assertObjectClass($tree, 'RaiseDomTreeNode');

        self::assertStrictEqual($tree->getByClass('hdr')->count(), 2);
        self::assertStrictEqual($tree->getByClass('subtitle')->count(), 1);
        self::assertStrictEqual($tree->getByClass('list')->count(), 1);
    }
}